<?php


namespace App\Http\Controllers\Admin;


use App\Models\LessonTestLevel;
use App\Models\LessonTest;

class LessonTestLevelController extends Controller
{
    /*
     * @api get /course-api/a/lessons/{lessonId}/test-levels 测试关卡列表
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function index($lessonId)
    {
        $this->validate($this->request, [
            'pageSize' => "integer",
            'page' => "integer|min:1",
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $query = LessonTestLevel::where('lesson_id', $lessonId);
        $total = $query->count();
        $testLevels = $query->orderBy('level', 'asc')->forPage($page, $pageSize)
            ->get(['id', 'level', 'description']);
        $items = [];
        if ($total) {
            /** @var LessonTestLevel $testLevelObj */
            foreach ($testLevels as $testLevelObj) {
                $items[] = [
                    'id' => $testLevelObj->id,
                    'level' => $testLevelObj->getLevel(),
                    'description' => $testLevelObj->getDescription(),
                ];
            }
        }
        return $this->sendSuccessJson(['total' => $total, 'items' => $items]);
    }

    /*
     * @api post /course-api/a/lessons/{lessonId}/test-levels 新增测试关卡
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param body int level 测试关卡
     * @param body string [description] 课时描述
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store($lessonId)
    {
        $this->request->merge(['lessonId' => $lessonId]);
        $this->validate($this->request, [
            'lessonId' => 'required|string',
            'level' => 'required|integer',
            'description' => 'string',
        ]);
        $level = $this->request->input('level');
        $description = $this->request->input('description', '');

        /** @var LessonTestLevel $testLevelObj */
        $testLevelObj =  new LessonTestLevel();
        $testLevelObj->setLevel($level);
        $testLevelObj->setDescription($description);
        $testLevelObj->setLessonId($lessonId);
        $testLevelObj->save();
        return $this->sendSuccessJson($testLevelObj->id);
    }

    /*
     * @api put /course-api/a/lessons/{lessonId}/test-levels/{levelId} 修改测试关卡
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param path int testId 测试ID
     * @param body string description 课时描述
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update($lessonId, $testId)
    {
        $this->request->merge(['id' => $testId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'description' => 'string',
        ]);
        $description = $this->request->input('description');
        /** @var LessonTestLevel $testLevelObj */
        $testLevelObj = LessonTestLevel::find($testId);
        $testLevelObj->setDescription($description);
        $testLevelObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /course-api/a/lessons/{lessonId}/test-levels/{levelId} 删除测试关卡
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param path int levelId 关卡ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function destroy($lessonId, $levelId)
    {
        LessonTestLevel::destroy(intval($levelId));
        LessonTest::where('lesson_id', $lessonId)->where('level_id', $levelId)->delete();
        return $this->sendSuccessJson();
    }
}